<template>
	<view class="app-container">
		<u-cell-group>
			<u-cell-item bg-color="#fffffF" title="修改登录密码" @click="toAccountSecurity">
				<u-image width="40rpx" height="40rpx" slot="icon"
					src="/static/image/home/icon_pwd_again.png"></u-image></u-cell-item>
			<u-cell-item bg-color="#fffffF" title="隐私协议" @click="openAgreementUrl()">
				<u-image width="40rpx" height="40rpx" slot="icon"
					src="/static/image/home/icon_private_agreement.png"></u-image>
			</u-cell-item>
		</u-cell-group>

		<!-- #ifndef H5 -->
		<!-- 	<u-cell-group>
			<u-cell-item title="清空缓存" :value="cacheSize" @click="clearCache" bg-color="#fffffF">
				<u-image width="40rpx" height="40rpx" slot="icon"
					src="/static/image/home/icon_private_agreement.png"></u-image>
			</u-cell-item>
		</u-cell-group> -->
		<!-- 	<u-cell-group>
			<u-cell-item title="版本号" :value="'V' + version" :arrow="false" bg-color="#fffffF">
				<u-image width="40rpx" height="40rpx" slot="icon"
					src="/static/image/home/icon_private_agreement.png"></u-image>
			</u-cell-item>
		</u-cell-group> -->
		<!-- #endif -->
		<!-- 退出登录 -->
		<u-button type="primary" :custom-style="customStyle" @click="handleSignOut">退出登录</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				customStyle: {
					// 注意驼峰命名，并且值必须用引号包括，因为这是对象
					backgroundColor: '#9D60FF',
					margin: '30rpx'
				},
				version: "",
				cacheSize: "",
				hasLogin: false,
			};
		},
		created() {
			// #ifndef H5
			this.getCacheSize();
			this.getVersion();
			// #endif

			if (uni.getStorageSync("token")) {
				this.hasLogin = true;
			}
		},
		methods: {
			openAgreementUrl() {
				let url = 'http://118.31.238.190/ysxy.html'
				uni.navigateTo({
					url: '/pages/main/webview/web_view?url=' + url
				})


			},
			handleSignOut() {
				uni.setStorageSync("token", null);
				uni.reLaunch({
					url: "/pages/main/login/login",
				});
			},
			toAccountSecurity() {
				uni.navigateTo({
					url: "/packageD/pages/change-password/change-password"
				});
			},
			getVersion() {
				this.version = plus.runtime.version;
			},
			getCacheSize() {
				//计算缓存
				plus.cache.calculate((size) => {
					if (size === 0) {
						this.cacheSize = "0MB";
					} else if (size < 1024) {
						this.cacheSize = size + "B";
					} else if (size < 1048576) {
						this.cacheSize = (size / 1024).toFixed(2) + "KB";
					} else if (size < 1073741824) {
						this.cacheSize = (size / 1048576).toFixed(2) + "MB";
					} else {
						this.cacheSize = (size / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			clearCache() {
				const _self = this;
				if (plus.os.name === "Android") {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					for (let i = 0; i < files.length; i++) {
						let filePath = "" + files[i];
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(
									function(entry) {
										uni.showToast({
											title: "清除成功",
										});
										_self.getCacheSize();
									},
									(errMsg) => {},
									(errMsg) => {}
								);
							} else {
								entry.remove();
							}
						});
					}
				} else {
					//IOS
					plus.cache.clear(
						() => {
							uni.showToast({
								title: "清除成功",
							});
							_self.getCacheSize();
						},
						(errMsg) => {}
					);
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.app-container {
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background-color: #ededed;
	}
</style>